﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class clientRegister : System.Web.UI.Page
    {
        ClientRegisterBUS clientregisterBus = new ClientRegisterBUS();
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    //loadData();
                    package();
                }
                catch (Exception)
                {
                }
            }
        }
        private void package()
        {
            Dropgoidichvu.Items.Clear();
            DataTable dt = new DataTable();
            dt.Columns.Add("namepackagelimit");
            dt = clientregisterBus.GetAllPackage();
            DataRow dr = dt.NewRow();
            dr["namepackagelimit"] = "Tất cả";
            dr["limitId"] = "-1";
            dt.Rows.InsertAt(dr, 0);
            //DataRow dr=new DataRow();
            //dr.colu
            Dropgoidichvu.DataSource = dt;
            Dropgoidichvu.DataTextField = "namepackagelimit";
            Dropgoidichvu.DataValueField = "limitId";
            Dropgoidichvu.DataBind();
            Dropgoidichvu.SelectedValue = "-1";

        }
        //private void loadData()
        //{
        //    try
        //    {
        //        //DateTime dt1 = new DateTime();
        //        DataTable t = clientregisterBus.Search_client_register("", "", "", "", "", "");
        //        dlPager.MaxPages = 1000;
        //        dlPager.PageSize = 50;
        //        dlPager.DataSource = t.DefaultView;
        //        dlPager.BindToControl = dtregister;
        //        this.dtregister.DataSource = dlPager.DataSourcePaged;
        //        this.dtregister.DataBind();


        //    }
        //    catch (Exception ex)
        //    { logs.Error(userLogin.Username + "-Client - LoadData", ex); }
        //}
        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            return null;
        }
        //protected void btnSave_Click(object sender, EventArgs e)
        //{

        //    string ngaydk_from = c_txtngaydangky.Text;
        //    string ngaydk_to = c_txtdenngaydangky.Text;
        //    string ngayhh_from = c_txtngayhethang.Text;
        //    string ngayhh_to = c_txtdenngayhethang.Text;
        //    DataTable t = clientregisterBus.Search_client_register(txtname.Text, Dropgoidichvu.SelectedValue, ngaydk_from, ngaydk_to, ngayhh_from, ngayhh_to);
        //    dlPager.MaxPages = 1000;
        //    dlPager.PageSize = 50;
        //    dlPager.DataSource = t.DefaultView;
        //    dlPager.BindToControl = dtregister;
        //    this.dtregister.DataSource = dlPager.DataSourcePaged;
        //    this.dtregister.DataBind();
        //    try
        //    {

        //    }
        //    catch (Exception ex)
        //    { logs.Error(userLogin.Username + "-Client - Filter", ex); }
        //}
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                ClientRegisterBUS eventBus = new ClientRegisterBUS();
                int eventId = id;
                //eventBus.Delete(eventId);

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-ListEvent - btnDelete_Click", ex);
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<clientRegisterdto> Search(string name, int packagelimitid, string ngaydk_from, string ngaydk_to, string ngayhh_from, string ngayhh_to)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            ClientRegisterBUS ctBUS = new ClientRegisterBUS();
            DataTable customerBySelect = ctBUS.Search_client_register(name, packagelimitid+"", ngaydk_from, ngaydk_to, ngayhh_from, ngayhh_to);
            List<clientRegisterdto> result = new List<clientRegisterdto>();
            clientRegisterdto item = new clientRegisterdto();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new clientRegisterdto();
                item.registerId = Convert.ToInt32(dr["registerId"] + "");
                item.ClientName = dr["ClientName"] + "";
                item.namepackagelimit = dr["namepackagelimit"] + "";
                item.Cost = Convert.ToDouble(dr["Cost"] + "");
                if (dr["from"] + "" != "")
                    item.from = Convert.ToDateTime(dr["from"] + "");
                if (dr["to"] + "" != "")
                    item.to = Convert.ToDateTime(dr["to"] + "");
                result.Add(item);
            }

            return result;
        }
    }
}